using System;
using System.Collections.Generic;
using UnityEngine;

public static class PanelName
{
    public static string PanelHall = "Panel_Hall";
    public static string PanelLoading = "Panel_Loading";
}

public class UIManager : ManagerBase
{
    private static UIManager _Instance;
    public static UIManager m_Instance
    {
        get { return _Instance ??= new UIManager(); }
    }

    private Dictionary<string, PanelBase> _panels = new Dictionary<string, PanelBase>();

    // 构造函数，新物体时会被调用
    public UIManager()
    {
        
    }

    // 析构函数，销毁对象时会调用
    ~UIManager()
    {
        _panels.Clear();
    }

    public void RegisterPanel(string pPanelName, PanelBase pPanel)
    {
        if (!_panels.ContainsKey(pPanelName))
        {
            _panels[pPanelName] = pPanel;
        }
        else
            Debug.LogWarning($"{pPanelName}已经被注册，检查代码是否有问题！！！");
    }

    public void DisregisterPanel(string pPanelName)
    {
        if (!_panels.ContainsKey(pPanelName))
        {
            _panels.Remove(pPanelName);
        }
    }

    public void OpenPanelByName(string pPanelName)
    {
        CloseOtherPanel();
        if (_panels.TryGetValue(pPanelName, out var curPanel))
        {
            curPanel.OpenPanel();
        }else
            Debug.LogWarning($"{pPanelName}界面不存在，所以没打开");
    }
    
    public void OpenPanelByName(string pPanelName, Action pAction)
    {
        CloseOtherPanel();
        if (_panels.TryGetValue(pPanelName, out var curPanel))
        {
            curPanel.OpenPanel(pAction);
        }else
            Debug.LogWarning($"{pPanelName}界面不存在，所以没打开");
    }

    public void ClosePanelByName(string pPanelName)
    {
        if (_panels.TryGetValue(pPanelName, out var curPanel))
        {
            curPanel.ClosePanel();
        }else
            Debug.LogWarning($"{pPanelName}界面不存在，所以没关闭");
    }

    private void CloseOtherPanel()
    {
        foreach (var item in _panels)
        {
            if (item.Value.m_PanelType != EPanelType.Hall)
            {
                item.Value.ClosePanel();
            }
        }
    }
}
